module.exports = {
	time(value, type) {
		console.log(value);
		value = parseInt(value)
		// console.log(value,'时间戳');
		let date = new Date(value);
		let y = date.getFullYear();
		let MM = date.getMonth() + 1;
		MM = MM < 10 ? "0" + MM : MM;
		let d = date.getDate();
		d = d < 10 ? "0" + d : d;
		let h = date.getHours();
		h = h < 10 ? "0" + h : h;
		let m = date.getMinutes();
		m = m < 10 ? "0" + m : m;
		let s = date.getSeconds();
		s = s < 10 ? "0" + s : s;
		if (type == 'YMD') {
			return y + "-" + MM + "-" + d;
		}
		if (type == 'YMDHMS') {
			return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
		}
		if (type == 'YMDHM') {
			return y + "-" + MM + "-" + d + " " + h + ":" + m;
		}
		if (type == 'YMDH') {
			return y + "-" + MM + "-" + d + " " + h;
		}
		return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
	},

	// 查看大图
	/**
	 * 参数一，要查看的图片数组，单个可直接传地址，【Array,String】
	 * 参数二，如果是数组需要查看的当前位置下标
	 */
	previmg(data, legnth = 0) {
		console.log('图', data)
		console.log('下标', legnth);
		var arr = []
		if (typeof data == 'object') {
			arr = [...data]
		} else {
			arr.push(data)
		}

		uni.previewImage({
			current: legnth,
			urls: arr || '',
		})
	},
	// 返回上一页
	navigateBack() {
		console.log('ewdf')
		var pages = getCurrentPages(); //当前页
		console.log(pages)
		if (pages.length < 2) {
			let a = pages[0].$page.fullPath;
			uni.reLaunch({
				// url: '/pages/index/index'
				url: a
			})
			return
		} else {
			// let b = pages.length - 2; 
			//  uni.redirectTo({
			// 	url: pages[b].$page.fullPath
			// })
			// uni.reLaunch({
			// 	// url: '/pages/index/index'
			// 	url: pages[b].$page.fullPath
			// })
			uni.navigateBack({
				delta:1
			});
		}


	},
	getNavWHdata() {
		console.log('获取导航信息');
		try {
			// 有时候getSystemInfoSync API在手机端会报错 所以我做了缓存，
			const value = uni.getStorageSync('statusBar_key_hk');
			if (value) {
				let obj = {
					statusBarHeight: value.statusBarHeight,
					navBarHeight: value.navBarHeight,
					windowWidth: value.windowWidth,
				}
				console.log('获取缓存导航信息成功');
				this.$store.dispatch('doctaddasync', obj)
				return
			}


			let obj = {}
			// 获取手机系统信息
			const info = uni.getSystemInfoSync()
			// 设置状态栏高度（H5顶部无状态栏小程序有状态栏需要撑起高度）
			obj.statusBarHeight = info.statusBarHeight
			obj.windowWidth = info.windowWidth
			obj.windowHeight = info.windowHeight

			// 获取胶囊的位置
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			console.log(menuButtonInfo);
			console.log(obj);
			// console.log(menuButtonInfo);
			// (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏内的高度) = 导航栏的高度
			obj.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info
				.statusBarHeight)
			obj.windowWidth = menuButtonInfo.left
			console.log(obj);
			this.$store.dispatch('doctaddasync', obj)
			uni.setStorage({
				key: 'statusBar_key_hk',
				data: obj,
				success: function() {
					console.log('缓存导航信息成功');
				}
			});
			return

		} catch (e) {
			console.log(e);
			//TODO handle the exception
			uni.showToast({
				title: '获取导航信息出错',
				icon: 'none'
			})
		}
	},
	// 判断登录态是否过期
	codeStatus() {
		const value = uni.getStorageSync('token_tiem');
		if (value) {
			let tiemState = (Date.now() - value.constTiem) / 1000
			if (tiemState >= value.exp_sec) {
				uni.removeStorageSync('token_tiem');
				uni.removeStorageSync('hk_token');
				try {
					uni.clearStorageSync();
				} catch (e) {
					// error
				}
				uni.showModal({
					title: '提示',
					content: '登录已过期，请重新登录',
					showCancel: false,
					success: (res) => {
						if (res.confirm) {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						}
					}
				});
			} else {

				// 获取用户信息
				this.userMD.getUserData.apply(this)
			}

		}
	},
	// 提示框
	showToast(title, icon) {
		uni.showToast({
			title,
			icon: icon ? '' : 'none'
		})
	}
}
